package com.start.common;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.flyqiu.common.tools.FlyQiuCollectionTools;
import com.flyqiu.common.user.DefaultFlyQiuUser;
import com.start.common.api.Result;
import com.start.common.entity.StartBaseUser;
import com.start.common.user.StartUserInfoLoadService;
import com.start.core.vo.RequestUserParams;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

@Tag(name = "用户信息管理")
@RestController
@RequestMapping("/start/commonUser")
public class StartCommonUserController {

    @GetMapping("/page")
    public Result<Page<DefaultFlyQiuUser>> pageUser(Page<DefaultFlyQiuUser> page, DefaultFlyQiuUser searchParams) {
        page = StartUserInfoLoadService.getInstance().page(page, searchParams);
        return Result.ok(page);
    }
    
    @PostMapping("/getUsers")
    public Result<?> getUsers(@RequestBody List<RequestUserParams> userParams) {
        if (FlyQiuCollectionTools.isEmpty(userParams)) {
            return Result.ok();
        }
        Map<String, List<String>> userTypeMap = userParams.stream().filter(e -> {
            if (StringUtils.isBlank(e.getUserType())) {
                return false;
            }
            return !StringUtils.isBlank(e.getId());
        }).collect(Collectors.groupingBy(RequestUserParams::getUserType, Collectors.mapping(RequestUserParams::getId, Collectors.toList())));
        StartUserInfoLoadService infoLoadService = StartUserInfoLoadService.getInstance();
        List<StartBaseUser> result = new ArrayList<>();
        for (String userType : userTypeMap.keySet()) {
            List<StartBaseUser> baseUsers = infoLoadService.getByUserIds(userType, userTypeMap.get(userType));
            for (StartBaseUser baseUser : baseUsers) {
                baseUser.setUserPwd(null);
            }
            result.addAll(baseUsers);
        }
        return Result.ok(result);
    }


}
